package com.qianbao.qianbaobusiness.ui.cashier;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.RxUtils;
import com.baseandroid.config.Global;
import com.baseandroid.config.MyApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.baseandroid.utils.TimeUtils;
import com.qianbao.qianbaobusiness.database.CityDao;
import com.qianbao.qianbaobusiness.database.DistrictDao;
import com.qianbao.qianbaobusiness.database.PcdDatabase;
import com.qianbao.qianbaobusiness.database.ProvinceDao;
import com.qianbao.qianbaobusiness.json.CashierBean;
import com.qianbao.qianbaobusiness.json.MerchantActivityBean;
import com.qianbao.qianbaobusiness.json.MerchantAdvertsBean;
import com.qianbao.qianbaobusiness.json.MerchantInfo;
import com.qianbao.qianbaobusiness.json.PcdBean;
import com.qianbao.qianbaobusiness.json.UnReadMsgBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author: Routee
 * @date 2018/6/11
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class CashierPresenter extends RxMvpPresenter<CashierView> {
    MyApi mApi;

    @Inject
    public CashierPresenter(MyApi api) {
        mApi = api;
    }

    public void getMsgUnread(String userNo, String userChannel, String type) {
        Map<String, String> params = new HashMap<>();
        params.put("userNo", userNo);
        params.put("userChannel", userChannel);
        params.put("type", type);
        mApi.getMsgUnread(params)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                //                .subscribeOn(Schedulers.newThread())
                //                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxObserver<Data<UnReadMsgBean>>() {
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().onNetError();
                    }

                    @Override
                    public void onNext(Data<UnReadMsgBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, true)) {
                            getView().getMsgUnReadSuccess(data.getResult());
                        } else {
                            getView().onNetError();
                        }
                    }
                });
    }

    public void getTotalStatics() {
        Map<String, String> params = new HashMap<>();
        params.put("merchantnos", getMerchantNos());
        params.put("date", getToday());
        params.put("tradeStatus", "97");
        params.put("payType", "99");
        params.put("pageNum", "0");
        params.put("pageSize", "10");
        mApi.getTotalStatics(params)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxObserver<Data<CashierBean>>() {
                    @Override
                    public void onNext(Data<CashierBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, true)) {
                            getView().getTotalStaticsSuccess(data.getResult());
                        } else {
                            getView().onNetError();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().onNetError();
                    }
                });
    }

    public void getMerchantsAdverts() {
        mApi.getMerchantsAdverts("20180621119452").compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<MerchantAdvertsBean>>() {
                    @Override
                    public void onNext(Data<MerchantAdvertsBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, false)) {
                            getView().getMerchantAdvertsSuccess(data.getResult());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void getMerchantActivity() {
        mApi.getMerchantActivity().compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<MerchantActivityBean>>() {
                    @Override
                    public void onNext(Data<MerchantActivityBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, false)) {
                            Global.setMerchantActivityBean(data.getResult());
                            getView().getMerchantActivitySuccess();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void getDistrict() {
        mApi.getDistrict(Global.getPcdVersion())
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.newThread())
                .subscribe(new RxObserver<Data<PcdBean>>() {
                    @Override
                    public void onNext(Data<PcdBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, false)) {
                            if (!data.getResult().getVersion().equals(Global.getPcdVersion())) {
                                Global.setPcdVersion(data.getResult().getVersion());
                                PcdDatabase pcdDatabase = PcdDatabase.getInstance(Global.getContext());
                                ProvinceDao provinceDao = pcdDatabase.provinceDao();
                                provinceDao.deleteAll();
                                List<PcdBean.ProvincesBean> provinces = data.getResult().getProvinces();
                                PcdBean.ProvincesBean[] provinceArray = new PcdBean.ProvincesBean[provinces.size()];
                                provinces.toArray(provinceArray);
                                provinceDao.insertAll(provinceArray);

                                CityDao cityDao = pcdDatabase.cityDao();
                                cityDao.deleteAll();
                                List<PcdBean.CitysBean> citys = data.getResult().getCitys();
                                PcdBean.CitysBean[] cityArray = new PcdBean.CitysBean[citys.size()];
                                citys.toArray(cityArray);
                                cityDao.insertAll(cityArray);

                                DistrictDao districtDao = pcdDatabase.districtDao();
                                districtDao.deleteAll();
                                List<PcdBean.DistrictsBean> districts = data.getResult().getDistricts();
                                PcdBean.DistrictsBean[] districtsArray = new PcdBean.DistrictsBean[districts.size()];
                                districts.toArray(districtsArray);
                                districtDao.insertAll(districtsArray);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });

    }

    private String getMerchantNos() {
        StringBuilder sb = new StringBuilder();
        List<MerchantInfo> merchantList = Global.getSelectedShopListInfo().merchantList;
        for (MerchantInfo bean : merchantList) {
            if (bean.isSelected) {
                sb.append(bean.merchantNo).append(",");
            }
        }
        if (sb.length() <= 0) {
            return "";
        }
        String str = sb.substring(0, sb.lastIndexOf(","));
        return str;
    }

    private String getToday() {
        String date = TimeUtils.getNowDate("yyyyMMdd", 0);
        return date;
    }
}
